<div class="container">

    <div class="card" >
        <div class="card-header">分类管理</div>


        <div class="form-inline" role="search">
            <div class="form-group">
                <input type="text" class="form-control name" placeholder="name" style="color: #000000">
            </div>
            <div class="form-group">
                <label for="pid" class="col-sm-4 control-label">分类</label>
                <div class="col-sm-8">

                    <select id="pid" class="form-control pid" style="color: #000000">
                        <option value="0">default</option>
                        #for(x : classes)
                        <option value="#(x.id)">#(x.name)</option>
                        #end
                    </select>
                </div>
            </div>
            <button type="submit" class="btn btn-light" onclick="CLASS.addClass()">新增</button>
        </div>
        <table class="table table-hover table-condensed">
            <tbody>
            <tr>
                <th>id</th>
                <th>上级</th>
                <th>名称</th>
                <th>操作</th>
            </tr>
            #for(x : data)
            <tr>
                <td>#(x.id)</td>
                <td>#(x.pname)</td>
                <td>#(x.name)</td>
                <td>
                    &nbsp;&nbsp;<a onclick="CLASS.deleteClass(#(x.id))">删除</a>
                    &nbsp;&nbsp;<a onclick="CLASS.changeClass(#(x.id))">修改</a>
                </td>
            </tr>
            #end
            </tbody>
        </table>
    </div>

    <script>

        const CLASS = {
            addClass: function addClass() {

                let name = $('.form-group .name').val(); //window.prompt("分类名称", "默认名称" + Date.now());

                let pid = $('.form-group .pid').val();//window.prompt("分类上级id", "0");

                console.log({name, pid})
                $.post("/class/save", JSON.stringify({"name": name, "pid": pid}), function (res) {
                    console.log({res})
                    if (res.state === 'ok') {
                        window.location.reload();
                    }
                });

            },
            changeClass: function changeClass(id) {
                let name = window.prompt("分类名称", "默认名称" + Date.now());
                $.post("/class/update", JSON.stringify({"name": name, "id": id}), function (res) {
                    console.log({res})
                    if (res.state === 'ok') {
                        window.location.reload();
                    }
                });

            },
            deleteClass: function deleteClass(id) {
                let b = window.confirm("确定删除?");
                if (b) {
                    window.location.href = "/class/delete/" + id;
                }
            }
        }
    </script>

</div>